define(function(require,exports,module) {
    var myPub=require('./public');
    var oWindow = $(window);
    var oIdNo = $('.js_idNo');
    var oWrap = $('.js_wrap');
    var oPhotoListLi = $('.js_photoList');
    var oReLoadCancel = $('.js_reLoadCancel');                  // 取消按钮
    var oReLoadUp = $('.js_reLoadUp');                          // 立即上传
    var oPhotoBox = $('.js_photoBox');
    var oIdCardNo = $('.js_idCardNo');                          // 身份证号码隐藏
    var oPreview = $('.js_preview');                            // 预览图片
    var oUpLoadTip = $('.js_upLoadTip');                        // 上传身份证按钮
    var oView = $('.js_view');                                  // 图片
    var oRightImg = $('.js_rightImg');                          // 上传成功标识
    var oIdCardHide = $('.js_idCardHide');                      // 订单详情页身份证号码
    var oReLoadUpNew = $('.js_reLoadUpNew');                    // 信息链接跳转的重新上传页面

    var bUpTap = true;
    var upTapTimer = null;
    var resizeTimer = null;

    var orderSnOdd = unescape(sessionStorage.getItem('orderSnOdd'));                        // 收货人名称
    var orderImageOdd = unescape(sessionStorage.getItem('orderImageOdd'));                  // 身份证照
    var orderIndCodeOdd = unescape(sessionStorage.getItem('orderIndCodeOdd'));              // 身份证号码
    hideNum(orderIndCodeOdd,oIdNo);
    hideNum(oIdCardHide.text(),oIdCardNo);
    // 改变尺寸时更新
    oWindow.resize(function () {
        if(resizeTimer){
            clearTimeout(resizeTimer);
        }
        resizeTimer = setTimeout(function () {
            setSize();
        },400);
    });
    setSize();
    if(orderImageOdd != '' && orderImageOdd.length>0 && orderImageOdd != 'null' && orderImageOdd != null){
        var imageStr = orderImageOdd.replace('[','');
        var imageRig = imageStr.replace(']','');
        var imageImg = imageRig.replace(' ','');
        var imageArr = imageImg.split(',');
        oView.each(function (index) {
            oView.eq(index).find('img').attr('src',httpImageUrl+imageArr[index]).css({'opacity':1});
            oPreview.eq(index).attr('src',httpImageUrl+imageArr[index]);
            // 显示上传成功标识
            oRightImg.eq(index).show();
            oUpLoadTip.eq(index).hide();
            oPhotoBox.eq(index).val(imageArr[index]);
        });
    }


    // 取消按钮
    oReLoadCancel.on('click',function () {
        window.history.back(-1);
    });
    // 立即上传
    oReLoadUp.on('click',function () {
        if(bUpTap){
            bUpTap = false;
            if(oPhotoBox.eq(0).val() == ''){
                myPub.TipLayer('请上传身份证正面照');
            }else if(oPhotoBox.eq(1).val() == ''){
                myPub.TipLayer('请上传身份证反面照');
            }else if(oPhotoBox.eq(0).val() == imageArr[0] && oPhotoBox.eq(1).val() == imageArr[1]){
                myPub.TipLayer('请上传清晰完整的身份证照片后点【立即上传】');
            }else{
                var photoFront = oPhotoBox.eq(0).val();
                var photoBack = oPhotoBox.eq(1).val();
                G_JSON_POST(base+'/m/o/1.0/order/globalOrderUploadImage',{
                    orderSn:orderSnOdd,
                    imagePath:photoFront+","+photoBack
                },function (data) {
                    if(data.ret.code == 0){
                        myPub.TipLayer('上传成功');
                        sessionStorage.removeItem('orderSnOdd');
                        sessionStorage.removeItem('orderNameOdd');
                        sessionStorage.removeItem('orderImageOdd');
                        sessionStorage.removeItem('orderIndCodeOdd');
                        setTimeout(function () {
                            location.href = base+'/shop/pay/order_view?orderSn='+orderSnOdd;
                        },3000)
                    }else{
                        myPub.TipLayer(data.ret.message);
                    }
                });
            }
        }
        clearTimeout(upTapTimer);
        upTapTimer = setTimeout(function () {
            bUpTap = true;
        },300)
    });

    // 信息链接跳转的重新上传页面立即上传
    oReLoadUpNew.on('click',function () {
        if(bUpTap){
            bUpTap = false;
            if(oPhotoBox.eq(0).val() == ''){
                myPub.TipLayer('请上传身份证正面照');
            }else if(oPhotoBox.eq(1).val() == ''){
                myPub.TipLayer('请上传身份证反面照');
            }else if(oPhotoBox.eq(0).val() == orderReverImg && oPhotoBox.eq(1).val() == orderFrontImg){
                myPub.TipLayer('请上传清晰完整的身份证照片后点【立即上传】');
            }else{
                var photoFront = oPhotoBox.eq(0).val();
                var photoBack = oPhotoBox.eq(1).val();
                G_JSON_POST(base+'/m/o/1.0/order/upLoadCardImage',{
                    orderSn:orderSnNo,
                    imagePath:photoFront+","+photoBack
                },function (data) {
                    if(data.ret.code == 0){
                        myPub.TipLayer('上传成功');
                    }else{
                        myPub.TipLayer(data.ret.message);
                    }
                });
            }
        }
        clearTimeout(upTapTimer);
        upTapTimer = setTimeout(function () {
            bUpTap = true;
        },300)
    });

    // 身份证图框大小
    function setSize() {
        var oWrap_W = oWrap.width();
        oPhotoListLi.css({
            'height':oWrap_W*0.284,
            'width':oWrap_W*0.448
        })
    }

// 这只身份证隐藏
    function hideNum(no,el) {
        var nfirst = no.slice(0, 4);
        var nend = no.slice(-4);
        el.text(nfirst + '**********' + nend);
    }
});